/**
 * 
 */
package com.yv.security.core.validate.code;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.yv.security.core.properties.SecurityProperties;
import com.yv.security.core.validate.code.image.ImageCodeGenerator;
import com.yv.security.core.validate.code.sms.DefaultSmsCodeSender;
import com.yv.security.core.validate.code.sms.SmsCodeSender;

/**
 * 可配置验证码生成逻辑
 * 举例:在security-demo里定义了一个类DemoImageCodeGenerator,并使用@Component("imageCodeGenerator")
 * 则此类里就不会再new默认的
 * @author zhailiang
 *
 */
@Configuration
public class ValidateCodeBeanConfig {
	
	private Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SecurityProperties securityProperties;
	
	@Bean
	@ConditionalOnMissingBean(name = "imageCodeGenerator")// If there's no bean named imageCodeGenerator, then execute the method
	public ValidateCodeGenerator imageValidateCodeGenerator() {
		logger.info("-----[ValidateCodeBeanConfig] imageValidateCodeGenerator() was called.");
		ImageCodeGenerator codeGenerator = new ImageCodeGenerator(); 
		codeGenerator.setSecurityProperties(securityProperties);
		return codeGenerator;
	}
	
	@Bean
	@ConditionalOnMissingBean(name = "smsCodeSender")
//	@ConditionalOnMissingBean(SmsCodeSender.class)  // If the app has found the implementation of this class, the following will not be executed
	public SmsCodeSender smsCodeSender() {
		logger.info("-----[ValidateCodeBeanConfig] smsCodeSender() was called.");
		return new DefaultSmsCodeSender(); 
	}
	

}
